//
//  FindMapViewController.m
//  CwinCar
//
//  Created by dllo on 15/10/21.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "FindMapViewController.h"
#import "BaseHeader.h"
#import <BaiduMapAPI/BMapKit.h>
@interface FindMapViewController ()<BMKMapViewDelegate>
{
    UIView * _headView ;
    
}

@property (nonatomic, strong)BMKMapView *mapView;//地图View类
@property (nonatomic , strong)BMKPinAnnotationView* annotationView ;
@property (nonatomic, strong) BMKPointAnnotation * annotation ;
@property (nonatomic, strong) BMKPointAnnotation *myAnnotation ;
@property (nonatomic, strong) BMKPointAnnotation * annotation2 ;
@property (nonatomic, strong) BMKPointAnnotation * annotation3 ;
@end

@implementation FindMapViewController
- (void)viewWillAppear:(BOOL)animated{
    
    [_mapView viewWillAppear];
    _mapView.delegate = self;// 此处记得不用的时候需要置nil，否则影响内存的释放
}
- (void)viewDidDisappear:(BOOL)animated{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil;//不用时,置nil
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,mainScreen_Width , 64 * suitableScreen_Width)];
    [_headView setBackgroundColor:[UIColor colorWithRed:0.088 green:0.331 blue:1.000 alpha:1.000]];
    [self.view addSubview:_headView];
    
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width / 2.42, mainScree_Height / 24.703, mainScreen_Width / 3.75, mainScree_Height / 22.233)];
    [_headView addSubview:titleLab];
    [titleLab setText:@"地图"];
    titleLab.textColor = [UIColor whiteColor];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:backButton];
    [backButton setFrame:CGRectMake(mainScreen_Width / 22.058, mainScree_Height / 24.703, mainScreen_Width / 12.5, mainScree_Height / 22.233)];
    [backButton setTintColor:[UIColor redColor]];
    [backButton setTitleColor:[UIColor colorWithRed:0.088 green:0.331 blue:1.000 alpha:1.000] forState:UIControlStateSelected];
    if (self.mapView == nil) {
        
        
        self.mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 64 * suitableScreen_Width, mainScreen_Width, mainScree_Height - 64 * suitableScreen_Width)];
    }
    [self.view addSubview:self.mapView];
    
    //普通矢量图
    [self.mapView setMapType:BMKMapTypeStandard];
    // 地图比例尺级别，在手机上当前可使用的级别为3-19级
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"distance"]isEqualToString:@"distance"]) {
        
        [self.mapView setZoomLevel:_cale];
        
    }else{
        [self.mapView setZoomLevel:13];
    }
    //地图旋转角度，在手机上当前可使用的范围为－180～180度
    [self.mapView setRotation:0];
    //地图俯视角度，在手机上当前可使用的范围为－45～0度
    [self.mapView setOverlooking:-30];
    //显示比例尺
    self.mapView.showMapScaleBar = YES;
    //自定义比例尺的位置
    [self.mapView setMapScaleBarPosition:CGPointMake(self.mapView.frame.size.width - 70, self.mapView.frame.size.height - 40)];
    
}
#pragma mark - 地图标注
- (void)viewDidAppear:(BOOL)animated{
    //添加一个BMKPointAnnotation(百度地图SDK预置的标注点)
    self.annotation = [[BMKPointAnnotation alloc]init];
    
    self.myAnnotation = [[BMKPointAnnotation alloc]init];
    
    CLLocationCoordinate2D coor;
    //设置经纬度
    NSString * latStr = self.model.lat ;
    NSString * lonStr = self.model.lon ;
    double lat = [latStr doubleValue];
    double lon = [lonStr doubleValue] ;
    coor.latitude = lat;//纬度
    coor.longitude = lon;//经度
    
    //    self.annotation2.coordinate
    [_mapView setCenterCoordinate:CLLocationCoordinate2DMake(lat, lon)];
    //我的位置
    CLLocationCoordinate2D myCoor;
    myCoor.latitude = self.myLat ;
    myCoor.longitude = self.myLon ;
    self.myAnnotation.coordinate = myCoor ;
   
    self.myAnnotation.title = @"我的位置";
    //该点坐标
    self.annotation.coordinate = coor;
    self.annotation.title = self.model.address;
    self.annotation.subtitle = self.model.shortname;
    [_mapView addAnnotation:self.annotation];
    [_mapView addAnnotation:self.myAnnotation];
}
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        //        //大头针标注View(BMKPinAnnotationView)
        NSString *annotationViewID = @"renameMark";
        _annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewID];
        if (_annotationView == nil) {
            _annotationView = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationViewID];
                       //设置颜色
//            if (annotation == _myAnnotation) {
//                NSLog(@"~~~~~~~~~");
//            }
//            else
//            {
//                NSLog(@"!!!!!!!!!");
//            }
            _annotationView.pinColor = BMKPinAnnotationColorRed;
            //设置动画
            _annotationView.animatesDrop = YES;
            //设置可拖拽
            //            annotationView.draggable = YES;
            
        }
        return _annotationView;
        
    }
    
    return nil;
}
- (void)backAction:(UIButton *)backBut
{
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.hidden = YES ;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
